Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add template context processors. #1904

Merged
merged 20 commits into from
Dec 29, 2022

Conversation

alex-oleshkevich
Copy link
Member

This PR adds template context processors.

import typing

from starlette.requests import Request
from starlette.templating import Jinja2Templates

def app_context(request: Request) -> typing.Dict[str, typing.Any]:
    return {'app': request.app}

templates = Jinja2Templates(directory='templates', context_processors=[
    app_context,
])

Async processors are not supported.

@Kludex
Copy link
Member

Kludex commented Oct 12, 2022

What's the motivation for it? You can still perform an analogous logic without introducing the context_processors, correct?

@alex-oleshkevich
Copy link
Member Author

Yes, by copypasting. If you share variables with all templates (for example, teams in sidebar, account balance, etc) you have to generate and pass these variables to the template in every view. Context processors solve this problem. You define login in one place and template's context get automatically updated.

Copy link
Member

@Kludex Kludex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docs/templates.md Outdated Show resolved Hide resolved
docs/templates.md Outdated Show resolved Hide resolved
docs/templates.md Outdated Show resolved Hide resolved
docs/templates.md Outdated Show resolved Hide resolved
docs/templates.md Outdated Show resolved Hide resolved
docs/templates.md Show resolved Hide resolved
docs/templates.md Outdated Show resolved Hide resolved
docs/templates.md Outdated Show resolved Hide resolved
@Kludex Kludex added feature New feature or request staticfiles Static file serving labels Dec 25, 2022
alex-oleshkevich and others added 6 commits December 25, 2022 21:41
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Kludex
Kludex previously approved these changes Dec 25, 2022
Copy link
Member

@Kludex Kludex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote a suggestion to simplify a bit the test, and the documentation.

Everything cool here. 👍

docs/templates.md Outdated Show resolved Hide resolved
tests/test_templates.py Outdated Show resolved Hide resolved
tests/test_templates.py Outdated Show resolved Hide resolved
alex-oleshkevich and others added 3 commits December 26, 2022 00:00
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
tests/test_templates.py Outdated Show resolved Hide resolved
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
@alex-oleshkevich
Copy link
Member Author

Are you waiting for anything from my side, @Kludex ?

@Kludex
Copy link
Member

Kludex commented Dec 29, 2022

I was kind a waiting for you to press merge 👀

@alex-oleshkevich alex-oleshkevich merged commit 008e3a5 into encode:master Dec 29, 2022
@alex-oleshkevich alex-oleshkevich deleted the context-processors branch December 29, 2022 14:41
invisibleroads added a commit to invisibleroads/invisibleroads-macros-web that referenced this pull request Feb 11, 2023
aminalaee pushed a commit that referenced this pull request Feb 13, 2023
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Viicos pushed a commit to Viicos/starlette that referenced this pull request Feb 16, 2023
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request staticfiles Static file serving
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants